package com.jzr.open.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.open.db1.model.TVitalSign;
import com.jzr.open.db1.service.VitalSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/vitalSign")
public class VitalSignController {
    @Autowired
    private VitalSignService vitalSignService;

    @PostMapping("/get")
    public Result<List<TVitalSign>> get(@RequestBody TVitalSign record){
        if(record == null){
            return Result.fail("参数不能为空");
        }
        if(U.isBlank(record.getPatientCode())){
            return Result.fail("patientCode不能为空");
        }
        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        qw.orderByAsc("patient_code","patient_cure_no","op_date","op_time");
        qw.eq(TVitalSign.PatientCode_,record.getPatientCode());

        if(U.isNotBlank(record.getPatientCureNo())){
            qw.eq(TVitalSign.PatientCureNo_,record.getPatientCureNo());
        }
        if(U.isNotBlank(record.getOpDate())){
            qw.eq(TVitalSign.OpDate_,record.getOpDate());
        }
        if(U.isNotBlank(record.getOpTime())){
            qw.eq(TVitalSign.OpTime_,record.getOpTime());
        }
        List<TVitalSign> list = vitalSignService.list(qw);
        return Result.success("成功",list,list==null ? 0:list.size());
    }

    @PostMapping("/post")
    public Result<Integer> post(@RequestBody List<TVitalSign> list){
        if(list == null || list.isEmpty()){
            return Result.fail("数据不能为空");
        }
        for(TVitalSign record : list){
            if(U.isBlank(record.getVitalNo())){
                return Result.fail("vitalNo不能为空");
            }
            if(U.isBlank(record.getPatientCode())){
                return Result.fail("patientCode不能为空");
            }
            if(U.isBlank(record.getPatientCureNo())){
                return Result.fail("patientCureNo不能为空");
            }
            if(U.isBlank(record.getOpDate())){
                return Result.fail("opDate不能为空");
            }
            if(U.isBlank(record.getOpTime())){
                return Result.fail("opTime不能为空");
            }
        }
        int count = vitalSignService.save(list);
        if(count <= 0){
            return Result.fail("保存失败");
        }else {
            return Result.success("成功",count,count);
        }
    }
//    @PostMapping("/delete")
//    public Result<Integer> delete(@RequestBody List<TVitalSign> list){
//        if(list == null || list.isEmpty()){
//            return Result.fail("数据不能为空");
//        }
//        for(TVitalSign record : list){
//            if(U.isBlank(record.getVitalNo())){
//                return Result.fail("vitalNo不能为空");
//            }
//        }
//        int count = vitalSignService.delete(list);
//        if(count <= 0){
//            return Result.fail("删除失败");
//        }else {
//            return Result.success("成功",count);
//        }
//    }
}
